package com.suixun.controller.credits;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.authority.controller.BaseManager;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.suixun.service.credits.CreditReordService;
import com.suixun.service.credits.CreditTaskService;
import com.suixun.util.Constant;
import com.suixun.util.Utils;
import com.suixun.vo.credits.BaseVo;
import com.suixun.vo.credits.CreditsGetVo;
import com.suixun.vo.credits.RequestPageVo;

/**
 * 积分记录(明细)
 * 
 * @author yh
 * @date 2017年11月17日
 */
@RestController
@RequestMapping(value = "/credits/reord")
public class CreditReordController extends BaseManager {

    // 积分记录
    @Autowired
    private CreditReordService reordService;

    // 积分任务
    @Autowired
    private CreditTaskService taskService;

    /**
     * 查询积分概况
     */
    @RequestMapping(value = "find_condition.json", method = RequestMethod.GET)
    public Object findCondition() {
        try {
            return success(reordService.findSumInfo());
        } catch (Exception e) {
            logger("CreditReordController findCondition:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 通过用户手机号分页查询积分情况
     * 
     * @param tel
     *            用户手机号
     * @param pageParam
     *            分页参数
     * @return
     */
    @RequestMapping(value = "find_page_tel.json", method = RequestMethod.GET)
    public Object findPageTel(@RequestParam("tel") String tel, RequestPageVo param) {
        try {
            if (!Utils.isTel(tel) || param.getStart() == null || param.getLength() == null) {
                return fail("参数错误!");
            }
            return success(reordService.findPageByTel(param, tel));
        } catch (Exception e) {
            logger("CreditReordController findPageTel:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 按照日期分页查看积分领取情况
     * 
     * @param param
     *            查询参数
     * @return
     */
    @RequestMapping(value = "find_page_info.json", method = RequestMethod.GET)
    public Object findPageGetInfo(CreditsGetVo param) {
        try {
            if (param.getStartDate() == null || param.getEndDate() == null || param.getStart() == null
                || param.getLength() == null) {
                return fail("参数错误!");
            }
            return success(reordService.findPageGetInfo(param));
        } catch (Exception e) {
            logger("CreditReordController findPageGetInfo:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 导出积分领取情况
     * 
     * @param param
     *            查询参数
     * @return
     */
    @RequestMapping(value = "export_get_info", method = RequestMethod.GET)
    public Object exportGetInfo(CreditsGetVo param) {
        try {
            if (param.getStartDate() == null || param.getEndDate() == null) {
                return fail("参数错误!");
            }
            List<BaseVo> taskNames = taskService.findAllName().getList();
            List<Map<String, Object>> list = reordService.findAllGetInfo(param);

            HSSFWorkbook workbook = exportDeviceList("积分领取情况", exportGetInfoTitle(taskNames),
                                                     exportGetInfoContent(taskNames, list));
            HttpServletResponse response = this.getResponse();
            response.setHeader("Content-disposition", "attachment;filename=get_credits_list.xls");
            workbook.write(this.getResponse().getOutputStream());
            return success();
        } catch (Exception e) {
            logger("CreditReordController exportGetInfo:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 积分领取情况的表头
     * 
     * @param taskNames
     *            需要转换的表头
     * @return
     */
    private ArrayList<String> exportGetInfoTitle(List<BaseVo> taskNames) {
        List<String> nameList = Lists.transform(taskNames, new Function<BaseVo, String>() {
            @Override
            public String apply(BaseVo vo) {
                return vo.getName();
            }
        });
        ArrayList<String> title = Lists.newArrayList(nameList);
        title.add(0, "今日领取");
        title.add(0, "日期");
        title.add("累积领取");
        return title;
    }

    /**
     * 积分领取情况的内容
     * 
     * @param taskNames
     *            表头
     * @param list
     *            需要转换的内容
     * @return
     */
    private List<List<String>> exportGetInfoContent(List<BaseVo> taskNames, List<Map<String, Object>> list) {
        List<List<String>> content = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            List<String> line = new ArrayList<>();
            Map<String, Object> map = list.get(i);
            line.add(map.get("doDate").toString());
            line.add(map.get("etotalNum").toString());
            for (int j = 0; j < taskNames.size(); j++) {
                line.add(map.get("task"+taskNames.get(j).getId()).toString());
            }
            line.add(map.get("cumulativeCollection").toString());
            content.add(line);
        }
        return content;
    }

}
